// pages/douban/douban-duo/douban-duo.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:"",
    url:"",
    hidden:false,
    num : 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    })
    switch (options.name) {
      case "正在上映":
        var url = app.url + '/v2/movie/in_theaters';
        this.request(url)
        break;
      case "即将上映":
        var url = app.url + "/v2/movie/coming_soon";
        this.request(url)
        break;
      default:
        var url = app.url + "/v2/movie/top250";
        this.request(url)
        console.log("热门电影")
    }
    wx.setNavigationBarTitle({
      title: options.name,
    })
  },

  // 下拉触底加载新的电影
  onReachBottom:function(){
    wx.showLoading()
    this.data.num += 9;
    var that=this;
    wx.request({
      url: this.data.url + "?start=" + this.data.num +"&count=3",
      success:function(e){
        wx.hideLoading()
        var list = that.data.list;
        if (e.data.subjects.length){
          for (var i = 0; i < e.data.subjects.length; i++) {
            e.data.subjects[i].num = 5;
            e.data.subjects[i].rating.stars.substr(0, 1)
          }
          list = list.concat(e.data.subjects)
          that.setData({
            list: list
          })
        }else{
          wx.showToast({
            title: '全部已加载',
            icon: 'none',
            duration: 500
          })
        }
      }
    })
  },
  request:function(url){
    var that = this;
    wx.request({
      url: url,
      success:function(e){
        wx.hideLoading()
        console.log(e.data)
        for (var i = 0; i < e.data.subjects.length; i++) {
          e.data.subjects[i].rating.stars = e.data.subjects[i].rating.stars.substr(0, 1);
          e.data.subjects[i].num = 5;
        }
        that.setData({
          list:e.data.subjects,
          hidden:true,
          url:url
        })
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '加载中...',
    })
    this.request(this.data.url)
    this.setData({
      num:0
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  }

})